﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Xml.Linq;

namespace SteamGameApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Game> games = new ObservableCollection<Game>();

        public MainWindow()
        {
            InitializeComponent();
            games =  (ObservableCollection<Game>)DataManager<Game>.Load("gamesList.xml");
            foreach (var game in games)
            {
                game.isDirty = false;
            }
            lbGames.ItemsSource = games;
        }
        
        private void btnAddGame_Click(object sender, RoutedEventArgs e)
        {
            games.Add(new Game() { Name = "" });
        }
        private void btnDeleteGame_Click(object sender, RoutedEventArgs e)
        {
            List<Game> toBeDeleted = new List<Game>();
            foreach (var game in lbGames.SelectedItems)
            {
                toBeDeleted.Add(game as Game);
            }
            foreach (var item in toBeDeleted)
            {
                games.Remove(item);
            }


        }
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            bool dirtyRecords = false;
            foreach (var g in games)
            {
                if (g.isDirty)
                {
                    dirtyRecords = true;
                    break;
                }
                    
            }
            if (dirtyRecords)
            {
                MessageBoxResult result = MessageBox.Show("Are you sure you want to quit?", "Quit", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    DataManager<Game>.Save(games, "gamesList.xml");
                    e.Cancel = false;
                }
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            DataManager<Game>.Save(games, "gamesList.xml");
            MessageBox.Show("Save Complete");
        }

        private void btnGoToBudgetWindow_Click(object sender, RoutedEventArgs e)
        {
            BudgetWindow budgetWindow = new BudgetWindow();
            budgetWindow.Show();
            this.Close();
        }

    }
}

